/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cart.hibernate.staff;

/**
 *
 * @author BibooChung
 */

import cart.hibernate.HibernateUtil;
import cart.hibernate.authLevel.*;
import java.util.List; 
import java.util.Iterator; 
 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ManageStaff {
  
    /*public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        
        try{
           tx = session.beginTransaction();
           AuthLevel authLevel = new AuthLevel();
           authLevel.setAuthLevel("Admin");
           session.save(authLevel);
           
           Staff staff1 = new Staff("tom");
           Staff staff2 = new Staff("peter");
           
           staff1.setAuthLevel(authLevel);
           staff2.setAuthLevel(authLevel);
           session.save(staff1);
           session.save(staff2);
           
           tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        }finally {
           session.close(); 
        }
  
        
    }*/

    public List getAllStaff() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        List allStaff = null;
        try{
           tx = session.beginTransaction();
           allStaff = session.createQuery("FROM Staff").list(); 
           tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        }finally {
           session.close(); 
        }
        return allStaff;
    }
    
    /*public long addStaff(String password) { 
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        long staffId = -1;

        try {
            tx = session.beginTransaction();
            Staff staff = new Staff(password);
            staffId = (Long) session.save(staff);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return staffId;
    }*/
    
    public long addStaff(String password, int authLevelId) { 
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        long staffId = -1;

        try {
            tx = session.beginTransaction();
            AuthLevel authLevel = (AuthLevel)session.get(AuthLevel.class, authLevelId); 

            Staff staff = new Staff(password, authLevel);
            staffId = (Long) session.save(staff);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return staffId;
    }
    
    public void updateStaff(long staffId, String newPassword, int authLevelId) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();

            Staff staff = (Staff)session.get(Staff.class, staffId); 
            staff.setPassword(newPassword);

            AuthLevel authLevel = (AuthLevel)session.get(AuthLevel.class, authLevelId); 
            staff.setAuthLevel(authLevel);

            session.update(staff);
            
            tx.commit();
        }catch (HibernateException e) {
           if (tx!=null) tx.rollback();
           e.printStackTrace(); 
        }finally {
           session.close(); 
        }
    }

    public void deleteStaff(long staffId) {
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         Staff staff = (Staff)session.get(Staff.class, staffId); 
         session.delete(staff); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
    }



}
